#include<pthread.h>

class LockGuard 
{
public:
    LockGuard(pthread_mutex_t* mutex)
        :_mutex(mutex)
    { 
        pthread_mutex_lock(_mutex); //对象构造时自动获取锁
    }

    ~LockGuard()
    {
        pthread_mutex_unlock(_mutex); //对象析构时自动释放锁
    }

    pthread_mutex_t* _mutex;
};